<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type"
        content="text/html; charset=ISO-8859-1" />
  <title>Code Examples for Programming in Scala</title>
  <link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>

<div id="mainTitles"><h3>Code Examples for</h3><h2>Programming in Scala</h2></div>  <p><a href="../index.html">
    Return to chapter index
  </a></p>
  <h2>32 GUI Programming</h2>

    <li>32.1 <a href="#sec1">A first Swing application</a></li>
    <li>32.2 <a href="#sec2">Panels and layouts</a></li>
    <li>32.3 <a href="#sec3">Handling events</a></li>
    <li>32.4 <a href="#sec4">Example: Celsius/Fahrenheit converter</a></li>
    <li>32.5 <a href="#sec5">Conclusion</a></li>
  </ul>

  <h3><a name="sec1"></a>32.1 A first Swing application</h3>

  <pre><hr>
  import scala.swing._

<hr>
// In file <a href="../swing/FirstSwingApp.scala">swing/FirstSwingApp.scala</a>

  import scala.swing._

  object FirstSwingApp extends SimpleGUIApplication {
    def top = new MainFrame {
      title = "First Swing App"
      contents = new Button {
        text = "Click me"
      }
    }
  }

<hr>
  object FirstSwingApp extends SimpleGUIApplication {

<hr>
    def top = new MainFrame {

<hr>
      title = "First Swing App"

<hr>
  def title: String

<hr>
  def title_=(s: String)

<hr>
      contents = new Button {

<hr>
        text = "Click me"

<hr>
  </pre>
  <h3><a name="sec2"></a>32.2 Panels and layouts</h3>

  <pre><hr>
// In file <a href="../swing/SecondSwingApp.scala">swing/SecondSwingApp.scala</a>

import scala.swing._

object SecondSwingApp extends SimpleGUIApplication {
  def top = new MainFrame {
    title = "Second Swing App"
    val button = new Button {
      text = "Click me"
    }
    val label = new Label {
      text = "No button clicks registered"
    }	
    contents = new BoxPanel(Orientation.Vertical) {
      contents += button
      contents += label
      border = Swing.EmptyBorder(30, 30, 10, 30)
    }
  }
}  

<hr>
  val label = new Label {
    text = "No button clicks registered"
  }	

<hr>
  contents = new BoxPanel(Orientation.Vertical) {

<hr>
  contents += button
  contents += label

<hr>
  border = Swing.EmptyBorder(30, 30, 10, 30)

<hr>
  </pre>
  <h3><a name="sec3"></a>32.3 Handling events</h3>

  <pre><hr>
  listenTo(button)

<hr>
  case class ButtonClicked(source: Button)

<hr>
  var nClicks = 0
  reactions += {
    case ButtonClicked(b) =&gt;
      nClicks += 1
      label.text = "Number of button clicks: "+ nClicks
  }

<hr>
// In file <a href="../swing/ReactiveSwingApp.scala">swing/ReactiveSwingApp.scala</a>

import scala.swing._
import scala.swing.event._

object ReactiveSwingApp extends SimpleGUIApplication {
  def top = new MainFrame {
    title = "Reactive Swing App"
    val button = new Button {
      text = "Click me"
    }
    val label = new Label {
      text = "No button clicks registered"
    }	
    contents = new BoxPanel(Orientation.Vertical) {
      contents += button
      contents += label
      border = Swing.EmptyBorder(30, 30, 10, 30)
    }
    listenTo(button)
    var nClicks = 0
    reactions += {
      case ButtonClicked(b) =&gt;
        nClicks += 1
        label.text = "Number of button clicks: "+ nClicks
    }
  }
}  

<hr>
// In file <a href="../swing/TempConverter.scala">swing/TempConverter.scala</a>

import swing._
import event._

object TempConverter extends SimpleGUIApplication {
  def top = new MainFrame {
    title = "Celsius/Fahrenheit Converter"
    object celsius extends TextField { columns = 5 }
    object fahrenheit extends TextField { columns = 5 }
    contents = new FlowPanel {
      contents += celsius
      contents += new Label(" Celsius  =  ")
      contents += fahrenheit
      contents += new Label(" Fahrenheit")
      border = Swing.EmptyBorder(15, 10, 10, 10)
    }
    listenTo(celsius, fahrenheit)
    reactions += {
      case EditDone(`fahrenheit`) =&gt;
        val f = fahrenheit.text.toInt
        val c = (f - 32) * 5 / 9
        celsius.text = c.toString
      case EditDone(`celsius`) =&gt;
        val c = celsius.text.toInt
        val f = c * 9 / 5 + 32
        fahrenheit.text = f.toString
    }     
  }
}

<hr>
  </pre>
  <h3><a name="sec4"></a>32.4 Example: Celsius/Fahrenheit converter</h3>

  <pre><hr>
  import swing._
  import event._

<hr>
  import scala.swing._
  import scala.swing.event._

<hr>
   case EditDone(`celsius`)

<hr>
  </pre>
  <h3><a name="sec5"></a>32.5 Conclusion</h3>


 <table>
 <tr valign="top">
 <td>
 <div id="moreinfo">
 <p>
 For more information about <em>Programming in Scala</em> (the "Stairway Book"), please visit:
 </p>
 
 <p>
 <a href="http://www.artima.com/shop/programming_in_scala">http://www.artima.com/shop/programming_in_scala</a>
 </p>
 
 <p>
 and:
 </p>
 
 <p>
 <a href="http://booksites.artima.com/programming_in_scala">http://booksites.artima.com/programming_in_scala</a>
 </p>
 </div>
 </td>
 <td>
 <div id="license">
 <p>
   Copyright &copy; 2007-2008 Artima, Inc. All rights reserved.
 </p>

 <p>
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
 </p>

 <p style="margin-left: 20px">
   <a href="http://www.apache.org/licenses/LICENSE-2.0">
     http://www.apache.org/licenses/LICENSE-2.0
   </a>
 </p>

 <p>
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
   implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 </p>
 </div>
 </td>
 </tr>
 </table>

</body>
</html>
